/*
* Implement queue operations 
*/ 
#include "../inc/queue.h"
#include "../inc/log.h"

/* create a queue */
Queue* queue_create() {
    Queue* q = malloc(sizeof(Queue));
    q->head = q->tail = NULL;
    q->size = 0;
    return q;
}

/* free a queue */
void queue_destroy(Queue *q) {
    Node *cur;
    while(q->head){
        cur = q->head;
        q->head = cur->next;
        free(cur);
    }
    free(q);
}

/* push a data into a queue */
void queue_push(Queue *q, void *data) {
    Node *node = (Node *)malloc(sizeof(Node));
    node->data = data;
    node->next = NULL;
    if (q->tail == NULL) {
        q->head = q->tail = node;
    } else {
        q->tail->next = node;
        q->tail = node;
    }
    q->size++;
}

/* pop a data into a queue */
void* queue_pop(Queue *q) {
    if (q->head == NULL) return NULL;
    Node *tmp = q->head;
    void *data = q->head->data;
    q->head = q->head->next;
    if (q->head == NULL) q->tail = NULL;
    free(tmp);
    q->size--;
    return data;
}

/* check whether the queue is empty */
int is_empty(Queue *q) {
    return (q->head == NULL);
}

/* getting the size of queue */
int queue_size(Queue *q){
    return q->size;
}


